package com.accp.demo.service.finance;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.accp.demo.mapper.finance.IPrepayMapper;
import com.accp.demo.pojo.PageBean;
import com.accp.demo.pojo.finance.Advance;
import com.accp.demo.pojo.finance.Prepay;
import com.accp.demo.pojo.finance.PrepayDetail;
import com.accp.demo.pojo.sale.SaleOutStorage;
import com.accp.demo.util.TestUtil;

/**
 * 预付款
 * @author Administrator
 *
 */
@Service
public class IPrepayService {
	
	@Autowired
	private IPrepayMapper prepayMapper;
	
	/**
	 * 新增预付款单
	 * @param pojo
	 */
	@Transactional
	public Boolean saveOrUpdate(Map<String, Object> map){
		try {
			if(map.get("createDate").toString().isEmpty()){
				//新增
				this.prepayMapper.addPrepay(map);	
				System.out.println("我在新增主表");
				this.prepayMapper.addPrepayDetail(map);
			}else{
				//修改
				System.out.println("我在修改记录");
				this.prepayMapper.updPrepay(map);
				this.prepayMapper.delPrepayDetail(map);
				this.prepayMapper.addPrepayDetail(map);
			}
			//this.prepayMapper.addPrepay(map);
			//this.prepayMapper.addPrepayDetail(map);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/**
	 * 查询所有主详记录
	 * @return
	 */
	public List<Prepay> findAll(){
		return this.prepayMapper.findAll();
	}
	
	/**
	 * 分页
	 * @param map
	 * @return
	 */
	public PageBean findByPage(int currentPage,int pageSize){
		Integer num=this.prepayMapper.queryCount();
		if(num==0){
			num=1;
		}
		List<Prepay> list=this.prepayMapper.findByPage((currentPage-1)*pageSize, pageSize);
		PageBean pageBean = new PageBean(pageSize, num, currentPage, list);
		return pageBean;
	}
	
	/**
	 * 根据单据日期生成单据号码
	 * @param map
	 * @return
	 */
	public String getNewNo(Map<String, Object> map){
		return TestUtil.getNewNo(this.prepayMapper.getLastNo(map));
	}
	
	/**
	 * 删除时逻辑修改状态
	 * @param map
	 * @return
	 */
	public Boolean updLogicalState(Map<String, Object> map){
		try {
			this.prepayMapper.updLogicalState(map);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/**
	 * 修改审核后等字段
	 * @param map
	 * @return
	 */
	public Boolean updPrepayExamineAfter(Map<String, Object> map){
		try {
			map.put("auditDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			//修改主表审核后的字段
			System.out.println("在service准备审核");
			this.prepayMapper.updPrepayExamineAfter(map);
			//修改供应商主文件
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
}
